﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using Please_starve.Common.Managers;
using Please_starve.Common.Interpolators;

namespace Please_starve.Cameras
{
    public static class Camera
    {
        static public Matrix viewMatrix;
        static public Matrix projectionMatrix;
        static public Viewport viewport;

        static Vector3 currentZoom;
        static Vector3 maxCameraZoom = new Vector3(0, 1000, 1000);

        static Vector3Interpolator interpolatorZoom = new Vector3Interpolator();

        public static Vector3 CurrentZoom
        {
            get { return currentZoom; }
            set
            {
                if (value.Y < 15)
                {
                    if (!interpolatorZoom.IsActive) interpolatorZoom.Start(currentZoom, value, 1f, true);
                    //currentZoom = new Vector3(0, MathHelper.Clamp(value.Y, 5f, maxCameraZoom.Y), MathHelper.Clamp(15, 5f, maxCameraZoom.Z));
                }
                else
                {
                    if (!interpolatorZoom.IsActive) interpolatorZoom.Start(currentZoom, value, 1f, true);
                    //currentZoom = new Vector3(0, MathHelper.Clamp(value.Y, 5f, maxCameraZoom.Y), MathHelper.Clamp(value.Z, 5f, maxCameraZoom.Z));
                }
            }
        }

        static Camera()
        {
            viewport = GameServices.GetService<GraphicsDevice>().Viewport;

            currentZoom = new Vector3(0, 3, 5);

            viewMatrix = Matrix.CreateLookAt(currentZoom, Vector3.Zero, Vector3.Up);

            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 5.0f / 3.0f, 0.1f, 10000f);
        }

        public static void Update(GameTime gameTime)
        {
            if (interpolatorZoom.IsActive)
            {
                currentZoom = interpolatorZoom.Update(gameTime);
            }
        }

        public static void LookAt(Vector3 position)
        {
            // Calculate the position the camera is looking from.
            Vector3 cameraPosition = currentZoom + position;

            viewMatrix = Matrix.CreateLookAt(cameraPosition, position, Vector3.Up);
        }
    }
}